//
//  MCPaymentCodeViewController.swift
//  StoreManage
//
//  Created by MC on 2017/12/27.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCPaymentCodeViewController: MCViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func saveButtonClikced() {
        let image = MCImage_createdWithView(view: bgView)
        
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(savedPhotosAlbum(image:didFinishSavingWithError:contextInfo:)), nil)
    }
    
    @objc func savedPhotosAlbum(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {
        
        if error != nil {
            MCHUD_text(message: "保存失败", view: self.view, delay: 1.5)
        } else {
            MCHUD_text(message: "已保存至相册", view: self.view, delay: 1.5)
        }
    }
    
    
    // MARK: - Network
    func sendNetworking() {
        
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "收款码"
        self.view.backgroundColor = UIColor.white
    }
    
    func initUI() {
        
        view.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
        
        bgView.addSubview(wxImageView)
        wxImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.centerY.equalTo(view.snp.bottom).offset(-40)
            make.size.equalTo(CGSize.init(width: 149, height: 37))
        }
        
        bgView.addSubview(bgImageView)
        bgImageView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(view)
            make.bottom.equalTo(-70)
        }
        
        bgView.addSubview(promptImageView)
        promptImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.centerY.equalTo(view.snp.top).offset(MCAdaptationModel(70))
            make.size.equalTo(CGSize.init(width: MCAdaptationModel(286), height: MCAdaptationModel(31)))
        }
        
        
        let W = MCScreenWidth*0.55
        
        bgView.addSubview(whiteView)
        whiteView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgImageView)
            make.centerY.equalTo(bgImageView).offset(10)
            make.size.equalTo(CGSize.init(width: W + 30, height: W + 15 + 60))
        }
        
        whiteView.addSubview(qrImageView)
        qrImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(15)
            make.height.equalTo(W)
        }
        
        whiteView.addSubview(storeLabel)
        storeLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(whiteView)
            make.top.equalTo(qrImageView.snp.bottom)
            make.bottom.equalTo(whiteView)
        }

        let url = H5_wxPay + "storeId=" + String(MCUserinfo_getStoreId())

        qrImageView.image = MCImage_createQRForString(qrString: url, iconImage: nil)

        storeLabel.text = MCUserinfo_getStoreName() + "收款码"

        view.addSubview(saveButton)
        saveButton.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(-90)
            make.size.equalTo(CGSize.init(width: 300, height: 40))
        }
    }
    
    
    // MARK: - Setter & Getter
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Store_pay_bg")
        return imageView
    }()
    
    lazy var promptImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Store_pay_prompt")
        return imageView
    }()
    
    lazy var whiteView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 5
        view.layer.masksToBounds = true
        return view
    }()
    
    
    lazy var qrImageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    lazy var storeLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(15)
        label.textColor = UIColor.MCBlack
        return label
    }()
    
    lazy var saveButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setImage(#imageLiteral(resourceName: "Store_download"), for: UIControlState.normal)
        button.setTitle(" 保存至相册", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(saveButtonClikced), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    
    lazy var wxImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Store_pay_wx")
        return imageView
    }()
    

}
